/*
 *  sky.h
 *  
 *
 *  Created by Jason Hsu, Jian Wei Gan, and Weiping Zhang on 10/25/09.
 *  Copyright 2009. All rights reserved.
 *
 */
#include "texture.h"
#include "util/color4f.h"

#include <string>
#include <sstream>
#include <list>
#include <stdio.h>

#define HOUR_TO_USE_DAWN_SKY 6
#define HOUR_TO_USE_DUSK_SKY 18

class Sky
{
private:
	weather_t *myCurrentWeather;	//current weather information
	weather_t myPreviousWeather;	//previous weather information
	Texture* myTexture;				//sky image texture
	GLfloat myRotation;				//angle of rotation of skycan
	GLUquadric* quad;				//cylinder quadric
	Color4f myBaseColor;			//base color of can
	GLfloat myColorOffset;			//color offset due to time of day?
	Vec3f myLocation;				//location of skycan in space
	struct tm* myTimeInfo;			//time information
	
public:
	Sky(weather_t* weather)
	{
		myCurrentWeather = weather;
		myPreviousWeather = *myCurrentWeather;
        if(*myCurrentWeather == RAINY || *myCurrentWeather == CLOUDY)
            myTexture = new Texture("sky_images/cloudysky.jpg");
        else 
        {
            myTimeInfo = Utility::getTime();
            if (myTimeInfo->tm_hour < HOUR_TO_USE_DAWN_SKY 
                || myTimeInfo->tm_hour > HOUR_TO_USE_DUSK_SKY)
                myTexture = new Texture("sky_images/nightsky.jpg");
            else if (myTimeInfo->tm_hour == HOUR_TO_USE_DAWN_SKY 
                     || myTimeInfo->tm_hour == HOUR_TO_USE_DUSK_SKY)
                myTexture = new Texture("sky_images/dawnsky.jpg");
            else {
                myTexture = new Texture("sky_images/sunnysky.jpg");
            }
        
        }
		myRotation = 0.0f;
		quad = gluNewQuadric();
		myBaseColor = Color4f(0.9f, 0.9f, 0.9f);
		myColorOffset = 0.0f;
		myLocation = Vec3f(0.0f, 53.0f, 0.0f);
	}
	
	~Sky()
	{
		delete myTexture;
		gluDeleteQuadric(quad);
	}
	
	
	/*
	 *	draw the sky.  enable and bind textures, then perform
	 *	affine transformations on "skycan" to move it behind
	 *	the terrain.
	 *
	 */
	void draw()
	{	
		myTexture->enable();
		myTexture->bind();
		
		glPushMatrix();
		
		myBaseColor.add(myColorOffset);
		glColor3f(myBaseColor[0], myBaseColor[1], myBaseColor[2]);
		myBaseColor.add(-myColorOffset);
		gluQuadricTexture(quad, true);
		glTranslatef(myLocation[0], myLocation[1], myLocation[2]);
		glScalef(3.0f, 1.0f, 1.0f);
		glRotatef(myRotation, 0.0f, 1.0f, 0.0f);
		glRotatef(90.0, 1.0f, 0.0f, 0.0f);
		gluCylinder(quad, 100, 100, 80, 20, 20);
		
		glPopMatrix();
		
		myTexture->disable();
	}
	
	void update()
	{
		myRotation += 0.05f;
		if(myRotation > 360.0f)
			myRotation = 0.0f;
		
		if(*myCurrentWeather != myPreviousWeather)
		{
			delete myTexture;
			
			if(*myCurrentWeather == RAINY || *myCurrentWeather == CLOUDY)
				myTexture = new Texture("sky_images/cloudysky.jpg");
			else 
			{
				myTimeInfo = Utility::getTime();
				if (myTimeInfo->tm_hour < HOUR_TO_USE_DAWN_SKY 
				   || myTimeInfo->tm_hour > HOUR_TO_USE_DUSK_SKY)
					myTexture = new Texture("sky_images/nightsky.jpg");
				else if (myTimeInfo->tm_hour == HOUR_TO_USE_DAWN_SKY 
				         || myTimeInfo->tm_hour == HOUR_TO_USE_DUSK_SKY)
					myTexture = new Texture("sky_images/dawnsky.jpg");
				else {
					myTexture = new Texture("sky_images/sunnysky.jpg");
				}
			}
            myPreviousWeather = *myCurrentWeather;	
		}
	}
};
